<template>
  <el-dialog
    v-if="visible"
    v-el-drag-dialog
    title="提示"
    :visible.sync="visible"
    width="500px"
    append-to-body
    @closed="close"
  >
    <div class="tip">
      <p>提示：当前实退金额大于应退金额，请注明原因后继续操作！</p>
      <el-form ref="formReason" :inline="true" :rules="rules" :model="form" class="form">
        <el-form-item label="退单原因:" prop="reason">
          <el-input
            v-model.trim="form.reason"
            class="backreason"
            type="textarea"
            :rows="2"
            placeholder="请输入"
            maxlength="50"
            clearable
          />
        </el-form-item>
      </el-form>
    </div>
    <div slot="footer" class="dialog-footer">
      <el-button @click="reset">
        取消
      </el-button>
      <el-button type="primary" @click="success">
        确定
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
  export default {
    name: 'backReason',
    data() {
      return {
        visible: false,
        form: {
          reason: ''
        },
        rules: {
          reason: [{ required: true, message: '请输入退款原因', trigger: 'change' }]
        }
      }
    },
    methods: {
      async open(temp) {
        return new Promise(resolve => {
          this.resolve = resolve
          this.visible = true
        })
      },
      close() {
        this.form = {
          reason: ''
        }
      },
      reset() {
        this.close()
        this.$refs['formReason'].clearValidate()
        this.$refs['formReason'].resetFields()
        this.visible = false
      },
      success() {
        this.$refs.formReason.validate(async(valid) => {
          if (!valid) return
          this.resolve(this.form.reason)
          this.visible = false
        })
      }
    }
  }
</script>
<style lang="scss" scoped>
.tip {
  color: red;
  p {
    margin-bottom: 20px;
  }
}
.backreason {
  width: 300px;
}
</style>
